<!doctype html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="-技术 -面向对象语言 -this的指向," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="What is this ?this到底是什么，之前我在写到执行上下文环境时，说到this也是其中的一个对象，还有arguments,点我传送,this是JS语言当中的关键字，在C# , java中也有此关键字，使用的方法也类似。这里先抛出一个结论：this的使用，是为了更优雅的传递对一个对象的引用。可以极大的简化代码的结构，当然，如果用不好就会很懵逼了。例一：1234567function a">
<meta name="keywords" content="-技术 -面向对象语言 -this的指向">
<meta property="og:type" content="article">
<meta property="og:title" content="当我们在调用this的时候，调用的到底是什么？">
<meta property="og:url" content="https://github.com/remarkableRide/io/2017/05/23/当我们在调用this的时候，调用的到底是什么？/index.html">
<meta property="og:site_name" content="RemarkableRide">
<meta property="og:description" content="What is this ?this到底是什么，之前我在写到执行上下文环境时，说到this也是其中的一个对象，还有arguments,点我传送,this是JS语言当中的关键字，在C# , java中也有此关键字，使用的方法也类似。这里先抛出一个结论：this的使用，是为了更优雅的传递对一个对象的引用。可以极大的简化代码的结构，当然，如果用不好就会很懵逼了。例一：1234567function a">
<meta property="og:updated_time" content="2017-05-23T14:38:24.412Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="当我们在调用this的时候，调用的到底是什么？">
<meta name="twitter:description" content="What is this ?this到底是什么，之前我在写到执行上下文环境时，说到this也是其中的一个对象，还有arguments,点我传送,this是JS语言当中的关键字，在C# , java中也有此关键字，使用的方法也类似。这里先抛出一个结论：this的使用，是为了更优雅的传递对一个对象的引用。可以极大的简化代码的结构，当然，如果用不好就会很懵逼了。例一：1234567function a">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://github.com/remarkableRide/io/2017/05/23/当我们在调用this的时候，调用的到底是什么？/"/>





  <title> 当我们在调用this的时候，调用的到底是什么？ | RemarkableRide </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  














  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">RemarkableRide</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocapitalize="off" autocomplete="off" autocorrect="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://github.com/remarkableRide/io/2017/05/23/当我们在调用this的时候，调用的到底是什么？/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="remarkableRide">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="RemarkableRide">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                当我们在调用this的时候，调用的到底是什么？
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-05-23T09:50:20+08:00">
                2017-05-23
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计</span>
                
                <span title="字数统计">
                  1,341
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长</span>
                
                <span title="阅读时长">
                  5
                </span>
              
            </div>
          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="What-is-this"><a href="#What-is-this" class="headerlink" title="What is this ?"></a>What is this ?</h2><p>this到底是什么，之前我在写到执行上下文环境时，说到<code>this</code>也是其中的一个对象，还有<code>arguments</code>,<a href="https://remarkableride.github.io/2017/05/18/JS作用域/" target="_blank" rel="external">点我传送</a>,<code>this</code>是JS语言当中的关键字，在<code>C# , java</code>中也有此关键字，使用的方法也类似。这里先抛出一个结论：<code>this</code>的使用，是为了更优雅的传递对一个对象的引用。可以极大的简化代码的结构，当然，如果用不好就会很懵逼了。例一：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">a</span> (<span class="params"></span>) </span>&#123;</div><div class="line">	<span class="keyword">return</span> <span class="keyword">this</span>.name;</div><div class="line">&#125;</div><div class="line"><span class="keyword">var</span> me = &#123;</div><div class="line">	<span class="attr">name</span>:<span class="string">"帅的一塌糊涂"</span></div><div class="line">&#125;;</div><div class="line">a.call(me);    <span class="comment">//  "结果你懂的"</span></div></pre></td></tr></table></figure></p>
<p>而不使用<code>this</code>时，我们的函数就需要显式的用一个参数来传递对对象的引用：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">a</span>(<span class="params">context</span>)</span>&#123;</div><div class="line">	<span class="keyword">return</span> context.name;</div><div class="line">&#125;</div><div class="line"><span class="keyword">var</span> me = &#123;</div><div class="line">	<span class="attr">name</span>:<span class="string">"帅的一塌糊涂"</span></div><div class="line">&#125;;</div><div class="line">a(me);</div></pre></td></tr></table></figure></p>
<p>不使用<code>this</code>冒似看上去比使用更容易理解，但实际生产中代码远远没有这么简单，要是一直使用显式参数传递上下文对象会让代码变得混乱。所以，我们应该去深入<code>this</code>的用法，网上的关于<code>this</code>的用法，一般分为4类，1，直接调用；2，作为对象的方法调用；3，作为构造函数调用；4，显式绑定call，apply调用。我认为这四种实际可分为两种情况。</p>
<h4 id="Rule-No-1-this-总是指向最后调用它的那个对象："><a href="#Rule-No-1-this-总是指向最后调用它的那个对象：" class="headerlink" title="Rule.No.1: this 总是指向最后调用它的那个对象："></a><strong>Rule.No.1: <code>this</code> 总是指向最后调用它的那个对象</strong>：</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> b = <span class="number">1</span>,</div><div class="line">    c =&#123;</div><div class="line">       <span class="attr">b</span>:<span class="number">2</span></div><div class="line">&#125;;</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">a</span> (<span class="params"></span>) </span>&#123;</div><div class="line">	<span class="built_in">console</span>.log(<span class="keyword">this</span>.b)</div><div class="line">&#125;</div><div class="line">a();    <span class="comment">//  1</span></div><div class="line"><span class="built_in">window</span>.a === a;    <span class="comment">//  true</span></div><div class="line">c.a = a;</div><div class="line">c.a();    <span class="comment">//  2</span></div><div class="line"><span class="built_in">window</span>.c.a === c.a;    <span class="comment">//  true</span></div></pre></td></tr></table></figure>
<p>这个例子中，我加入了<code>window</code>对象便于理解，实际上我们定义在全局中的函数也是这样被调用，我们书写的时候可以省略掉<code>window</code>，像一种小的语法糖。先说这个例子的意外情况，在声明严格模式下，不允许<code>this</code>默认绑定到最外层的<code>window</code>对象上，也就是严格模式会输出<code>undefined</code>。而<code>window.c.a()</code>之所以会输出2，就是我们说的作为对象的方法调用，对应了规则一：<code>this</code>总是指向最后调用它的那个对象。即便c对象处于一个非全局作用域中(其他函数作用域)调用也是这个结果。</p>
<h4 id="Rule-No-2-使用call-apply调用函数时传递的第一个参数即为this的值，俗称‘硬绑定’："><a href="#Rule-No-2-使用call-apply调用函数时传递的第一个参数即为this的值，俗称‘硬绑定’：" class="headerlink" title="Rule.No.2: 使用call,apply调用函数时传递的第一个参数即为this的值，俗称‘硬绑定’："></a><strong>Rule.No.2: 使用<code>call,apply</code>调用函数时传递的第一个参数即为<code>this</code>的值，俗称‘硬绑定’：</strong></h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> a = <span class="number">1</span>,</div><div class="line">    b =&#123;</div><div class="line">       <span class="attr">a</span>:<span class="number">2</span>,</div><div class="line">    &#125;,</div><div class="line">    c = &#123;&#125;;</div><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span> (<span class="params"></span>) </span>&#123;</div><div class="line">	<span class="built_in">console</span>.log(<span class="keyword">this</span>.a);</div><div class="line">&#125;</div><div class="line">foo.call(b);    <span class="comment">//  2</span></div><div class="line">foo.call(a);    <span class="comment">//  undefined</span></div><div class="line">foo.call(c);    <span class="comment">//  undefined</span></div><div class="line">foo.call(<span class="keyword">this</span>);    <span class="comment">//  1</span></div><div class="line">foo.call();    <span class="comment">//  1</span></div></pre></td></tr></table></figure>
<p>调用<code>call</code>方法时，或者写入<code>this</code>为绑定到默认对象，此例中的默认对象为<code>window</code>。当作为构造函数调用时，指向构造函数也是<code>call</code>方法的变换，而<code>new</code>实例也是该方法的变种：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span> (<span class="params"></span>) </span>&#123;</div><div class="line">	<span class="keyword">this</span>.c=<span class="number">1</span>;</div><div class="line">&#125;</div><div class="line"><span class="keyword">var</span> a = <span class="keyword">new</span> foo();    <span class="comment">//  实际等价于下面代码</span></div><div class="line"><span class="keyword">var</span> b = &#123;&#125;;</div><div class="line">b.__proto__ = foo.prototype;</div><div class="line"><span class="keyword">var</span> result=foo.call(b);</div><div class="line"><span class="keyword">return</span> <span class="keyword">typeof</span> result ===<span class="string">'obj'</span> ? result : b;</div></pre></td></tr></table></figure></p>
<p>使用<code>new</code>关键字构造对象，等价于新建一个对象，把对象的原型绑定在函数的原型上，再到对象的作用域中调用函数，实际即把函数的<code>this</code>绑定到对象上，最后一步，检测返回值，网上好多关于<code>new</code>的定义都没写这一步，如果该函数有返回值，那么新建对象则应包含函数的返回值，否则就为对象本身。<em>可以看出，作为构造函数调用时，<code>this</code>的指向为<code>new</code>出来的对象，都是<code>call</code>方法的功劳。对应了规则2</em>。<br>再来看一个例子：<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">var</span> a = &#123;</div><div class="line">	<span class="attr">b</span> : <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123; <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123; <span class="built_in">console</span>.log(<span class="keyword">this</span>.c) &#125; &#125;,</div><div class="line">	<span class="attr">c</span>:<span class="number">3</span></div><div class="line">&#125;;</div><div class="line"><span class="keyword">var</span> c = <span class="number">5</span>;</div><div class="line"><span class="keyword">var</span> d = &#123; <span class="attr">c</span>:<span class="number">10</span> &#125;;</div><div class="line">a.b()();    <span class="comment">//  5</span></div><div class="line">a.b().call(a);    <span class="comment">//  3</span></div><div class="line">a.b().call(d);    <span class="comment">//  10</span></div></pre></td></tr></table></figure></p>
<p>有些教程认为，<code>a.b()()</code>调用时<code>this</code>绑定到最外层的<code>window</code>是规则之外，个人不敢苟同，此例中<code>a.b()</code>返回的是一个完整的函数，函数在全局作用域调用，自然<code>this</code>就绑定到了最外层，同理，用<code>call</code>方法改变作用域后，<code>this</code>也绑定到了<code>call</code>方法的第一个参数对象中。<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span> (<span class="params"></span>) </span>&#123;</div><div class="line">	<span class="built_in">console</span>.log(<span class="keyword">this</span>.c);</div><div class="line">&#125;</div><div class="line"><span class="keyword">var</span> a = &#123;</div><div class="line">	<span class="attr">c</span>:<span class="number">3</span>,</div><div class="line">	<span class="attr">foo</span>:foo</div><div class="line">&#125;;</div><div class="line"><span class="keyword">var</span> bar = a.foo;</div><div class="line"><span class="keyword">var</span> c = <span class="number">10</span>;</div><div class="line">bar();    <span class="comment">//  10</span></div><div class="line">a.foo();    <span class="comment">//  3</span></div></pre></td></tr></table></figure></p>
<p>这段代码来源于《你不知道的JavaScript》，书中将<code>bar()</code>输出为10定义为隐式丢失。这样其实比较难以理解，这里实际考察的是<code>=</code>函数赋值行为到底是在干什么，函数赋值给变量，变量中储存的实际上是指向<strong>函数本身</strong>内存的指针，与函数名无关。可以想象成匿名函数，函数名仅仅是一个标签，可随时更换。也就解释了<code>foo</code>函数依据调用环境的不同而输出不同的数字。</p>
<h3 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h3><p>闭包，原型链，<code>this</code>可以说是JS语言中的三座大山，也是这门语言的基石。无论怎么深入都不过分，尤其是在现在前端框架层出不穷的时候，<code>angular</code>,<code>vue</code>,<code>react</code>和各种JS库，能基础运用这些框架很简单，而想了解框架背后所蕴含的思想却大不简单。而不能了解一门语言的精髓，不了解一个框架的内部构造，始终都会被新出的框架牵着鼻子走，唯有深入JS内部，知晓它的长处，不足。才能在这些新新技术中做出合适的选择。</p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div>听说打赏的人会中500万!</div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="/images/money.png" alt="remarkableRide WeChat Pay"/>
          <p>微信打赏</p>
        </div>
      
      
    </div>
  </div>


      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/技术-面向对象语言-this的指向/" rel="tag"># -技术 -面向对象语言 -this的指向</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/05/18/JS作用域/" rel="next" title="JS作用域">
                <i class="fa fa-chevron-left"></i> JS作用域
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="lv-container" data-id="city" data-uid="MTAyMC8yODQ3MS81MDQy"></div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.gif"
               alt="remarkableRide" />
          <p class="site-author-name" itemprop="name">remarkableRide</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">6</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">5</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/remarkableRide" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        
          
             <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=35032655&auto=0&height=66"></iframe>
         
        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#What-is-this"><span class="nav-number">1.</span> <span class="nav-text">What is this ?</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Rule-No-1-this-总是指向最后调用它的那个对象："><span class="nav-number">1.0.1.</span> <span class="nav-text">Rule.No.1: this 总是指向最后调用它的那个对象：</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Rule-No-2-使用call-apply调用函数时传递的第一个参数即为this的值，俗称‘硬绑定’："><span class="nav-number">1.0.2.</span> <span class="nav-text">Rule.No.2: 使用call,apply调用函数时传递的第一个参数即为this的值，俗称‘硬绑定’：</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#小结"><span class="nav-number">1.1.</span> <span class="nav-text">小结</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">remarkableRide</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  






  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  


  




	





  





  





  
    <script type="text/javascript">
      (function(d, s) {
        var j, e = d.getElementsByTagName(s)[0];
        if (typeof LivereTower === 'function') { return; }
        j = d.createElement(s);
        j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
        j.async = true;
        e.parentNode.insertBefore(j, e);
      })(document, 'script');
    </script>
  






  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length == 0) {
      search_path = "search.xml";
    }
    var path = "/" + search_path;
    // monitor main search box;

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.popup').toggle();
    }
    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';
      $.ajax({
        url: path,
        dataType: "xml",
        async: true,
        success: function( xmlResponse ) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = $( "entry", xmlResponse ).map(function() {
            return {
              title: $( "title", this ).text(),
              content: $("content",this).text(),
              url: $( "url" , this).text()
            };
          }).get();
          var $input = document.getElementById(search_id);
          var $resultContent = document.getElementById(content_id);
          $input.addEventListener('input', function(){
            var matchcounts = 0;
            var str='<ul class=\"search-result-list\">';
            var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
            $resultContent.innerHTML = "";
            if (this.value.trim().length > 1) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var content_index = [];
                var data_title = data.title.trim().toLowerCase();
                var data_content = data.content.trim().replace(/<[^>]+>/g,"").toLowerCase();
                var data_url = decodeURIComponent(data.url);
                var index_title = -1;
                var index_content = -1;
                var first_occur = -1;
                // only match artiles with not empty titles and contents
                if(data_title != '') {
                  keywords.forEach(function(keyword, i) {
                    index_title = data_title.indexOf(keyword);
                    index_content = data_content.indexOf(keyword);
                    if( index_title >= 0 || index_content >= 0 ){
                      isMatch = true;
                      if (i == 0) {
                        first_occur = index_content;
                      }
                    }

                  });
                }
                // show search results
                if (isMatch) {
                  matchcounts += 1;
                  str += "<li><a href='"+ data_url +"' class='search-result-title'>"+ data_title +"</a>";
                  var content = data.content.trim().replace(/<[^>]+>/g,"");
                  if (first_occur >= 0) {
                    // cut out 100 characters
                    var start = first_occur - 20;
                    var end = first_occur + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if(start == 0){
                      end = 50;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    var match_content = content.substring(start, end);
                    // highlight all keywords
                    keywords.forEach(function(keyword){
                      var regS = new RegExp(keyword, "gi");
                      match_content = match_content.replace(regS, "<b class=\"search-keyword\">"+keyword+"</b>");
                    });

                    str += "<p class=\"search-result\">" + match_content +"...</p>"
                  }
                  str += "</li>";
                }
              })};
            str += "</ul>";
            if (matchcounts == 0) { str = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>' }
            if (keywords == "") { str = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>' }
            $resultContent.innerHTML = str;
          });
          proceedsearch();
        }
      });}

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched == false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(function(e){
      $('.popup').hide();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    });
    $('.popup').click(function(e){
      e.stopPropagation();
    });
  </script>





  

  

  

  

</body>
</html>
